define(['jquery', 'kindEditor'], function(require, exports, module) {
    function setLeftMenu() {
        require.async('./Single.js', function(a) {
            a.setLeftMenu();
        });
    }

    function initEditor() {
        KindEditor.ready(function(k) {
            window.editor = k.create('#content', {
                themeType: 'simple',
                uploadJson : '/Ajax/File/editorUpload',
                items : [
                    'bold','italic','underline','fontname','fontsize','forecolor','hilitecolor','plug-align','plug-order','plug-indent','image','multiimage'
                ]
            });
        })
    }

    function categorySelect(config) {
        var userBreedName = config.userBreedName || '';
        var defaultCategoryId = config.defaultCategoryId || '';

        $('#js-c1').on('change', function() {
            var o2 = $('#js-c2');
            var categoryId = $(this).val();
            if (!categoryId) {
                o2.html('<option value="">请选择分类</option>');
                return;
            }
            o2.html($('#js-category-data select[categoryId="' + categoryId + '"]').html());
        });
        if (userBreedName || defaultCategoryId) {
            $('#js-category-data').find('option').each(function () {
                var html = $(this).html();
                var cid = $(this).attr('value');
                html = $.trim(html);
                if (defaultCategoryId && defaultCategoryId == cid) {
                    var pid = $(this).parent().attr('categoryId');
                    var categoryId = $(this).attr('value');
                    $('#js-c1').val(pid).change();
                    $('#js-c2').val(categoryId);
                } else if (userBreedName == html) {
                    var pid = $(this).parent().attr('categoryId');
                    var categoryId = $(this).attr('value');
                    $('#js-c1').val(pid).change();
                    $('#js-c2').val(categoryId);
                }
            });
        }
    }

    function bindSubmit() {
        $('#js-article-edit').on('submit', function() {
            var errorObj = $('#js-error-box');
            errorObj.showError('');
            editor.sync();
            var categoryId = $('#js-c2').val();
            var title = $(this).find('input[name="title"]').val();
            var content = $('#content').val();
            if (!categoryId) {
                errorObj.showError('请选择分类！');
                return false;
            }
            if (!title) {
                errorObj.showError('请填写标题！');
                return false;
            }
            if (!content) {
                errorObj.showError('请填写内容！');
                return false;
            }
            var articleId = $(this).find('input[name="articleId"]').val();
            var data = {
                categoryId: categoryId,
                title: title,
                content: content,
                articleId: articleId
            };
            $.ajax({
                url: '/Ajax/Article/Save',
                data: data,
                type: 'post',
                dataType: 'json',
                success: function(d) {
                    if (d.error) {
                        alert(d.data);
                    } else {
                        articleId = d.data;
                        if (articleId) {
                            showArticleSave('保存成功',
                                '保存成功，马上<a href="/single/detail_' + articleId + '.html">预览</a>！',
                                articleId
                            );
                        } else {
                            showArticleSave('发布成功',
                                '发布成功，马上<a href="/single/detail_' + articleId + '.html">预览</a>！',
                                articleId
                            );
                        }
                    }
                }
            });
            return false;
        });
    }

    exports.init = function(config) {
        setLeftMenu();
        initEditor();
        categorySelect(config);
        bindSubmit();
    }
});